package com.syezon.wifikey.pwd.dialog;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.Gravity;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;

import com.syezon.android.base.utils.ScreenUtils;
import com.syezon.android.base.utils.ToastUtils;
import com.syezon.wifikey.R;
import com.syezon.wifikey.pwd.info.SaveWifiPwdInfo;
import com.syezon.wifikey.utils.ClipboardUtils;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by Administrator on 2018/9/11.
 */

public class OpSavePwdDialog extends Dialog {

    @BindView(R.id.tv_ssid)
    TextView mTvSsid;
    @BindView(R.id.tv_connect)
    TextView mTvConnect;
    @BindView(R.id.tv_copy_pwd)
    TextView mTvCopyPwd;
    @BindView(R.id.tv_del)
    TextView mTvDel;
    @BindView(R.id.tv_edit)
    TextView mTvEdit;
    @BindView(R.id.tv_cancel)
    TextView mTvCancel;

    private SaveWifiPwdInfo mSaveWifiPwdInfo;
    private EditPwdDialog mEditPwdDialog;

    private OpListener opListener;

    public OpSavePwdDialog(@NonNull Context context) {
        this(context, 0);
    }

    public OpSavePwdDialog(@NonNull Context context, int themeResId) {
        super(context, themeResId);
        setContentView(View.inflate(context, R.layout.dialog_save_pwd_info, null));
        ButterKnife.bind(this);
    }

    public void setSaveWifiPwdInfo(SaveWifiPwdInfo saveWifiPwdInfo) {
        this.mSaveWifiPwdInfo = saveWifiPwdInfo;
        mTvSsid.setText(saveWifiPwdInfo.getSsid());
    }

    public void setOpListener(OpListener opListener) {
        this.opListener = opListener;
    }

    @OnClick({R.id.tv_connect, R.id.tv_copy_pwd, R.id.tv_del, R.id.tv_edit, R.id.tv_cancel})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_connect:
                if (opListener != null) {
                    opListener.onConnect(mSaveWifiPwdInfo);
                }
                dismiss();
                break;
            case R.id.tv_copy_pwd:
                ClipboardUtils.copyText(mSaveWifiPwdInfo.getPwd());
                ToastUtils.showShort("复制成功！");
                dismiss();
                break;
            case R.id.tv_del:
                if (opListener != null) {
                    opListener.onDelete(mSaveWifiPwdInfo);
                }
                dismiss();
                break;
            case R.id.tv_edit:
                if (mEditPwdDialog == null) {
                    mEditPwdDialog = new EditPwdDialog(getContext(), R.style.Dialog);
                }
                mEditPwdDialog.setSaveWifiPwdInfo(mSaveWifiPwdInfo);
                mEditPwdDialog.setOpListener(opListener);
                mEditPwdDialog.show();
                dismiss();
                break;
            case R.id.tv_cancel:
                dismiss();
                break;
        }
    }

    @Override
    public void show() {
        getWindow().setGravity(Gravity.BOTTOM);
        getWindow().setLayout(ScreenUtils.getScreenWidth(), WindowManager.LayoutParams.WRAP_CONTENT);
        super.show();
    }

}
